package com.cy.aircompressor.main.home.systemsetting.fragment;

import android.os.Bundle;

import androidx.fragment.app.FragmentManager;

import com.cy.aircompressor.R;
import com.cy.aircompressor.main.home.systemsetting.fragment.child.DisplayInformationFragment;
import com.cy.aircompressor.main.home.systemsetting.fragment.child.PowerStationManagerFragment;
import com.cy.aircompressor.main.home.systemsetting.fragment.child.UserManagerFragment;
import com.cy.aircompressor.main.view.HeaderBar;
import com.cy.aircompressor.main.view.HeaderBarTab;
import com.drug.lib.util.DLog;
import com.drug.lib.util.DensityUtil;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.fragment.BaseVBDialogFragment;
import com.cy.aircompressor.databinding.DialogLifecycleLayoutBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import me.yokeyword.fragmentation.SupportFragment;


/**
 * Created by YoKeyword on 16/6/3.
 * 信息总览
 */
public class SystemSettingFragment extends BaseVBDialogFragment<DialogLifecycleLayoutBinding> {

    public static SystemSettingFragment showDialogFragment(FragmentManager fragmentManager, String tag) {
        Bundle args = new Bundle();
        SystemSettingFragment fragment = new SystemSettingFragment();
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
        return fragment;
    }

    @Override
    protected DialogLifecycleLayoutBinding getViewBinding() {
        return DialogLifecycleLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected int getWindowHeight() {
        return (getScreenHeight() * 3 / 5);
    }

    @Override
    protected int getWindowWidth() {
        return (super.getScreenWidth() * 3 / 5);
    }

    @Override
    protected void initialize() {
        initView();
        initFragments();
    }

    @Override
    protected void initData() {
        mBinding.titleName.setText("系统设置");
    }

    private List<SupportFragment> fragments = new ArrayList<>();

    private void initFragments() {
        SupportFragment firstFragment = findFragment(DisplayInformationFragment.class);
        if (firstFragment != null) {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用
            fragments.add(firstFragment);
            fragments.add(findFragment(PowerStationManagerFragment.class));
            fragments.add(findFragment(UserManagerFragment.class));
        } else {
            fragments.add(DisplayInformationFragment.newInstance());
            fragments.add(PowerStationManagerFragment.newInstance());
            fragments.add(UserManagerFragment.newInstance());
            loadMultipleRootFragment(R.id.fl_container, 0,
                    fragments.get(0),
                    fragments.get(1),
                    fragments.get(2));
        }
    }

    private void initView() {
        mBinding.headerBar
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_asxx)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_dzgl)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_yhgl)));
        mBinding.headerBar.setOnTabSelectedListener(new HeaderBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(fragments.get(position), fragments.get(prePosition));
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
            }
        });

        RxView.clicks(mBinding.closeBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    dismiss();
                });
    }

    @Override
    protected void requestNetwork() {

    }

}
